#include "compresor.h"

compresor::compresor(void)
{
    compression_start = false;
    decompression_start = false;
    forward_adaptation = true;
    bitmap = NULL;
    predictor = 0;
    data = NULL;
    dedata = NULL;
    parameters = new predictor_header;
    parameters->coefficients = NULL;
    block_size = 0;
}

compresor::~compresor(void)
{
    delete data;
    delete parameters->coefficients;
    delete parameters;
}

void compresor::new_bitmap(QImage* image)
{
    bitmap = image;
    if ((block_size > 32) || (block_size == 0))
    {
        if (bitmap->width() > bitmap->height())
            block_size = bitmap->width();
        else
            block_size = bitmap->height();

    }
}

void compresor::compression_run(QString path)
{
    if(bitmap != NULL)
    {
        if(!compression_start)
        {
            compression_path = path;
            compression_start = true;
        }
        else
            compression_start = false;
    }
}

void compresor::decompression_run(QString path)
{
    if(!decompression_start)
    {
        decompression_path = path;
        decompression_start = true;
    }
    else
        decompression_start = false;
}

void compresor::adaptation_changed_forward()
{
    forward_adaptation = true;
}

void compresor::adaptation_changed_backward()
{
    forward_adaptation = false;
}

void compresor::block_size_changed(int value)
{
    //qDebug() << "compressor size";
    if (value != 0)
        block_size = value;
    else
    {
        if (bitmap != NULL)
        {
            if (bitmap->width() > bitmap->height())
                block_size = bitmap->width();
            else
                block_size = bitmap->height();
        }
        else
            block_size = value;
    }
    //qDebug() << size;
}

void compresor::compresor_forward3(int size)
{

    int v=0;  //iterator pionowy blokow
    int h=0;  //iterator poziomy blokow
    int vi;   //iterator pionowy wewnatrz bloku
    int hi;   //iterator poziomy wewnatrz bloku
    int iter=0;
    int data_iter=0;
    short Rx,Gx,Bx;
    unsigned char X[3],L[3],G[3],S[3];
    float L2[3],G2[3],S2[3],LS[3],LG[3],GS[3],XL[3],XS[3],XG[3];
    float WL1[3],WL2[3],WL3[3],W[3];
    float Alfa0[3],Alfa1[3],Alfa2[3];
    parameters->bitmap.height = bitmap->height();
    parameters->bitmap.width = bitmap->width();
    parameters->segment_count = (ceil((double)parameters->bitmap.height/(double)size)) * (ceil((double)parameters->bitmap.width/(double)size));
    parameters->coefficient_count = 3;
    delete parameters->coefficients;
    parameters->coefficients = new unsigned short[parameters->segment_count*parameters->coefficient_count*3];
    parameters->segment_size = size;
    parameters->compression_type = 1;
    delete data;
    data = new short[parameters->bitmap.height*parameters->bitmap.width*3];
    int height=bitmap->height();
    int width=bitmap->width();

    for(v=0; v < height; v+=size)
    {
        for(h=0; h<width ; h+=size)
        {
              //qDebug()<<"kompresja dla obrazka x="<< h <<" "<< h+size << "y= "<< v <<" "<<v+size;
              //liczenie wspolczynnikow predykcji
              for(int i=0; i<3; i++)
              {
                 L2[i]=G2[i]=S2[i]=LS[i]=LG[i]=GS[i]=XL[i]=XS[i]=XG[i]=0;   //zerowanie mnoznikow
              }
              for(vi=v ; (vi<v+size) && (vi < height); vi++)
              {
                  for(hi=h; (hi<h+size) && (hi <width); hi++)
                  {
                    if((hi!=0) && (vi!= 0))
                    {
                        X[0]=qRed(this->bitmap->pixel(hi,vi));
                        L[0]=qRed(this->bitmap->pixel(hi-1,vi));
                        S[0]=qRed(this->bitmap->pixel(hi-1,vi-1));
                        G[0]=qRed(this->bitmap->pixel(hi,vi-1));
                        X[1]=qGreen(this->bitmap->pixel(hi,vi));
                        L[1]=qGreen(this->bitmap->pixel(hi-1,vi));
                        S[1]=qGreen(this->bitmap->pixel(hi-1,vi-1));
                        G[1]=qGreen(this->bitmap->pixel(hi,vi-1));
                        X[2]=qBlue(this->bitmap->pixel(hi,vi));
                        L[2]=qBlue(this->bitmap->pixel(hi-1,vi));
                        S[2]=qBlue(this->bitmap->pixel(hi-1,vi-1));
                        G[2]=qBlue(this->bitmap->pixel(hi,vi-1));
                        for(int i=0; i<3; i++)
                        {
                            L2[i]+=L[i]*L[i];
                            G2[i]+=G[i]*G[i];
                            S2[i]+=S[i]*S[i];
                            LS[i]+=L[i]*S[i];
                            LG[i]+=L[i]*G[i];
                            GS[i]+=G[i]*S[i];
                            XL[i]+=X[i]*L[i];
                            XS[i]+=X[i]*S[i];
                            XG[i]+=X[i]*G[i];
                        }
                    }
                 }
              }
              //wyliczanie wyznaczników i wartosci wspolczynnikow
              for(int i=0; i<3; i++)
              {
                 W[i]=L2[i]*G2[i]*S2[i]+2*LS[i]*GS[i]*LG[i]-G2[i]*LS[i]*LS[i]-L2[i]*GS[i]*GS[i]-S2[i]*LG[i]*LG[i];
                 WL1[i]=XL[i]*G2[i]*S2[i]+XG[i]*GS[i]*LS[i]+XS[i]*LG[i]*GS[i]-LS[i]*G2[i]*XS[i]-GS[i]*GS[i]*XL[i]-S2[i]*LG[i]*XG[i];
                 WL2[i]=L2[i]*XG[i]*S2[i]+LG[i]*XS[i]*LS[i]+LS[i]*XL[i]*GS[i]-LS[i]*XG[i]*LS[i]-GS[i]*XS[i]*L2[i]-S2[i]*XL[i]*LG[i];
                 WL3[i]=L2[i]*G2[i]*XS[i]+LG[i]*GS[i]*XL[i]+LS[i]*LG[i]*XG[i]-XL[i]*G2[i]*LS[i]-XG[i]*GS[i]*L2[i]-XS[i]*LG[i]*LG[i];
                 Alfa0[i]=WL1[i]/W[i];
                 Alfa1[i]=WL2[i]/W[i];
                 Alfa2[i]=WL3[i]/W[i];
              }
              //redukcja rozmiaru parametrow
              for(int i=0;i<3; i++)
              {
                  Alfa0[i]=this->short_to_float(this->float_to_short(Alfa0[i]));
//                  qDebug()<<"alfa0["<<i<<"] dla h="<<h<<" v="<<v<<"w koderze wynosi" <<Alfa0[i];
                  Alfa1[i]=this->short_to_float(this->float_to_short(Alfa1[i]));
//                  qDebug()<<"alfa1["<<i<<"] dla h="<<h<<" v="<<v<<"w koderze wynosi" <<Alfa0[i];
                  Alfa2[i]=this->short_to_float(this->float_to_short(Alfa2[i]));
//                  qDebug()<<"alfa2["<<i<<"] dla h="<<h<<" v="<<v<<"w koderze wynosi" <<Alfa0[i];
                  parameters->coefficients[iter]=float_to_short(Alfa0[i]);
                  iter++;
                  parameters->coefficients[iter]=float_to_short(Alfa1[i]);
                  iter++;
                  parameters->coefficients[iter]=float_to_short(Alfa2[i]);
                  iter++;
//                  parameters->coefficients[(v/size)+(h/size)*parameters->bitmap.width/size+parameters->coefficient_count*i] = float_to_short(Alfa0[i]);
//                  parameters->coefficients[(v/size)+(h/size)*parameters->bitmap.width/size+parameters->coefficient_count*i+1] = float_to_short(Alfa1[i]);
//                  parameters->coefficients[(v/size)+(h/size)*parameters->bitmap.width/size+parameters->coefficient_count*i+2] = float_to_short(Alfa2[i]);
              }
              /*
                Tutaj fragment dla ciebie Do zakodowana są zmienne typu unsigned short. Dekoder bedzie wiedział co z nimi zrobić
                bo przekonwertuje sobie spowrotem na float;
                Parametry dla każdego bloku:
                Alfa0[0]-pierwszy parametr na kanału Red
                Alfa1[0]-drugi parametr dla kanału Red
                Alfa2[0]-trzeci parametr dla kanały Red
                Alfa0[1]-pierwszy dla green
                Alfa1[1]-drugi dla green
                Alfa2[1]-trzei dla green
                Alfa0[2]-pierwszy dla blue
                Alfa1[2]-drugi blue
                Alfa2[2]-trzeci blue
                To są floaty, czyli przerzutuj najpierw na shorta:
                float_to_short(Alfa0[0])

              */
              //kodowanie bloku danych

              for(vi=v ; (vi<v+size) && (vi < height); vi++)
              {
                  for(hi=h; (hi<h+size) && (hi < width); hi++)
                  {
                        if((hi!=0) && (vi!= 0))   //blok nie na krawedzi
                        {
                               Rx=qRed(this->bitmap->pixel(hi,vi))-(int)(Alfa0[0]*qRed(this->bitmap->pixel(hi-1,vi))+Alfa1[0]*qRed(this->bitmap->pixel(hi,vi-1))+Alfa2[0]*qRed(this->bitmap->pixel(hi-1,vi-1)));
                               Gx=qGreen(this->bitmap->pixel(hi,vi))-(int)(Alfa0[1]*qGreen(this->bitmap->pixel(hi-1,vi))+Alfa1[1]*qGreen(this->bitmap->pixel(hi,vi-1))+Alfa2[1]*qGreen(this->bitmap->pixel(hi-1,vi-1)));
                               Bx=qBlue(this->bitmap->pixel(hi,vi))-(int)(Alfa0[2]*qBlue(this->bitmap->pixel(hi-1,vi))+Alfa1[2]*qBlue(this->bitmap->pixel(hi,vi-1))+Alfa2[2]*qBlue(this->bitmap->pixel(hi-1,vi-1)));

                        }
                        else if(hi!=0)
                        {
                            Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi-1,vi));
                            Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi-1,vi));
                            Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi-1,vi));

                           // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi gornej";
                        }
                        else if(vi!=0)
                        {
                            Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi,vi-1));
                            Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi,vi-1));
                            Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi,vi-1));
                            //qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi bocznej";
                        }
                        else   //gorny lewy piksel
                        {
                            Rx=qRed(this->bitmap->pixel(hi,vi));
                            Gx=qGreen(this->bitmap->pixel(hi,vi));
                            Bx=qBlue(this->bitmap->pixel(hi,vi));
                           // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest w gornym lewym rogu";
                        }
                        data[data_iter]=Rx;
                        data_iter++;
                        data[data_iter]=Gx;
                        data_iter++;
                        data[data_iter]=Bx;
                        data_iter++;
                        /*Tutaj otrzymujesz już zakodowane dane po wyjsciu z predyktora. Teraz są to shorty:
                          Rx-zakodowane z kanału Red
                          Gx-zakodowane z kanału Green
                          Bx-zakodowanie z kanału Blue
                        */
                       //qDebug()<<"x="<<hi<<" y= "<<vi<<Rx<<" "<<Gx<<" "<<Bx;
                  }
              }
        }
    }



}

void compresor::compresor_forward2(int size)
{
    int v=0;  //iterator pionowy blokow
    int h=0;  //iterator poziomy blokow
    int vi;   //iterator pionowy wewnatrz bloku
    int hi;   //iterator poziomy wewnatrz bloku
    int iter=0;
    int data_iter=0;
    short Rx,Gx,Bx;
    unsigned char X[3],L[3],G[3];
    float L2[3],G2[3],LG[3],XL[3],XG[3];
    float WL1[3],WL2[3],W[3];
    float Alfa0[3],Alfa1[3];
    //qDebug()<<size;
    parameters->bitmap.height = bitmap->height();
    parameters->bitmap.width = bitmap->width();
    parameters->segment_count = (ceil((double)parameters->bitmap.height/(double)size)) * (ceil((double)parameters->bitmap.width/(double)size));
    parameters->coefficient_count = 2;
    delete parameters->coefficients;
    parameters->coefficients = new unsigned short[parameters->segment_count*parameters->coefficient_count*3];
    parameters->segment_size = size;
    parameters->compression_type = 0;
    delete data;

    data = new short[parameters->bitmap.height*parameters->bitmap.width*3];

    for(v=0; v < this->bitmap->height(); v+=size)
    {
        for(h=0; h<this->bitmap->width() ; h+=size)
        {
              //qDebug()<<"kompresja dla obrazka x="<< h <<" "<< h+size << "y= "<< v <<" "<<v+size;
              //liczenie wspolczynnikow predykcji
              for(int i=0; i<3; i++)
              {
                 L2[i]=G2[i]=LG[i]=XL[i]=XG[i]=0;   //zerowanie mnoznikow
              }
              for(vi=v ; (vi<v+size) && (vi<this->bitmap->height()); vi++)
              {
                  for(hi=h; (hi<h+size) && (hi < this->bitmap->width()); hi++)
                  {
                    if((hi!=0) && (vi!= 0))
                    {
                        X[0]=qRed(this->bitmap->pixel(hi,vi));
                        L[0]=qRed(this->bitmap->pixel(hi-1,vi));
                        G[0]=qRed(this->bitmap->pixel(hi,vi-1));
                        X[1]=qGreen(this->bitmap->pixel(hi,vi));
                        L[1]=qGreen(this->bitmap->pixel(hi-1,vi));
                        G[1]=qGreen(this->bitmap->pixel(hi,vi-1));
                        X[2]=qBlue(this->bitmap->pixel(hi,vi));
                        L[2]=qBlue(this->bitmap->pixel(hi-1,vi));
                        G[2]=qBlue(this->bitmap->pixel(hi,vi-1));
                        for(int i=0; i<3; i++)
                        {
                            L2[i]+=L[i]*L[i];
                            G2[i]+=G[i]*G[i];
                            LG[i]+=L[i]*G[i];
                            XL[i]+=X[i]*L[i];
                            XG[i]+=X[i]*G[i];
                        }
                    }
                 }
              }
              //wyliczanie wyznaczników i wartosci wspolczynnikow
              for(int i=0; i<3; i++)
              {
                 W[i]=L2[i]*G2[i]-LG[i]*LG[i];
                 WL1[i]=G2[i]*XL[i]-LG[i]*XG[i];
                 WL2[i]=L2[i]*XG[i]-LG[i]*XL[i];
                 Alfa0[i]=WL1[i]/W[i];
                 Alfa1[i]=WL2[i]/W[i];
              }
              //redukcja rozmiaru parametrow
              for(int i=0;i<3; i++)
              {
                  Alfa0[i]=this->short_to_float(this->float_to_short(Alfa0[i]));
                  Alfa1[i]=this->short_to_float(this->float_to_short(Alfa1[i]));
            //      qDebug()<<"alfa0["<<i<<"] dla h="<<h<<" v="<<v<<"w koderze wynosi" <<Alfa0[i];
//                  parameters->coefficients[(v/size)+(h/size)*parameters->bitmap.width/size+parameters->coefficient_count*i] = float_to_short(Alfa0[i]);
//                  parameters->coefficients[(v/size)+(h/size)*parameters->bitmap.width/size+parameters->coefficient_count*i+1] = float_to_short(Alfa1[i]);
                  parameters->coefficients[iter]=float_to_short(Alfa0[i]);
                  iter++;
                  parameters->coefficients[iter]=float_to_short(Alfa1[i]);
                  iter++;
              }
              /*
                Tutaj fragment dla ciebie Do zakodowana są zmienne typu unsigned short. Dekoder bedzie wiedział co z nimi zrobić
                bo przekonwertuje sobie spowrotem na float;
                Parametry dla każdego bloku:
                Alfa0[0]-pierwszy parametr na kanału Red
                Alfa1[0]-drugi parametr dla kanału Red
                Alfa0[1]-pierwszy dla green
                Alfa1[1]-drugi dla green
                Alfa0[2]-pierwszy dla blue
                Alfa1[2]-drugi blue
                To są floaty, czyli przerzutuj najpierw na shorta:
                float_to_short(Alfa0[0])

              */
              //kodowanie bloku danych
              for(vi=v ; (vi<v+size) && (vi<this->bitmap->height()); vi++)
              {
                  for(hi=h; (hi<h+size) && (hi < this->bitmap->width()); hi++)
                  {
                        if((hi!=0) && (vi!= 0))   //blok nie na krawedzi
                        {
                               Rx=qRed(this->bitmap->pixel(hi,vi))-(int)(Alfa0[0]*qRed(this->bitmap->pixel(hi-1,vi))+Alfa1[0]*qRed(this->bitmap->pixel(hi,vi-1)));
                               Gx=qGreen(this->bitmap->pixel(hi,vi))-(int)(Alfa0[1]*qGreen(this->bitmap->pixel(hi-1,vi))+Alfa1[1]*qGreen(this->bitmap->pixel(hi,vi-1)));
                               Bx=qBlue(this->bitmap->pixel(hi,vi))-(int)(Alfa0[2]*qBlue(this->bitmap->pixel(hi-1,vi))+Alfa1[2]*qBlue(this->bitmap->pixel(hi,vi-1)));
                               //qDebug()<<"x="<<hi<<" y= "<<vi<<" oryginalnie czerwony= "<< qRed(bitmap->pixel(hi,vi)) <<" wyjscie predyktora " << Rx <<" Poprawka="<<(int)(Alfa0[0]*qRed(this->bitmap->pixel(hi-1,vi))+Alfa1[0]*qRed(this->bitmap->pixel(hi,vi-1)));

                        }
                        else if(hi!=0)
                        {
                            Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi-1,vi));
                            Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi-1,vi));
                            Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi-1,vi));
                           // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi gornej";
                        }
                        else if(vi!=0)
                        {
                            Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi,vi-1));
                            Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi,vi-1));
                            Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi,vi-1));

                            //qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi bocznej";
                        }
                        else   //gorny lewy piksel
                        {
                            Rx=qRed(this->bitmap->pixel(hi,vi));
                            Gx=qGreen(this->bitmap->pixel(hi,vi));
                            Bx=qBlue(this->bitmap->pixel(hi,vi));
                           // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest w gornym lewym rogu";
                        }
                        //                        data[(hi+vi*parameters->bitmap.width)*3] = Rx;
                        //                        data[(hi+vi*parameters->bitmap.width)*3+1] = Gx;
                        //                        data[(hi+vi*parameters->bitmap.width)*3+2] = Bx;
                        data[data_iter]=Rx;
                        data_iter++;
                        data[data_iter]=Gx;
                        data_iter++;
                        data[data_iter]=Bx;
                        data_iter++;
                        /*Tutaj otrzymujesz już zakodowane dane po wyjsciu z predyktora. Teraz są to shorty:
                          Rx-zakodowane z kanału Red
                          Gx-zakodowane z kanału Green
                          Bx-zakodowanie z kanału Blue
                        */
                       //qDebug()<<"x="<<hi<<" y= "<<vi<<Rx<<" "<<Gx<<" "<<Bx;
                  }
              }
        }
    }

}

void compresor::PNGm5(void)
{

    int vi;   //iterator pionowy pikseli
    int hi;   //iterator poziomy pikseli
    short pw[3],pg[3],pgl[3];
    short Rx,Gx,Bx;

    parameters->bitmap.height = bitmap->height();
    parameters->bitmap.width = bitmap->width();
    parameters->segment_count = 0;
    parameters->coefficient_count = 0;
    delete parameters->coefficients;
    parameters->coefficients = NULL;
    parameters->segment_size = 0;
    parameters->compression_type = 2;
    delete data;
    data = new short[parameters->bitmap.height*parameters->bitmap.width*3];

    for(vi=0 ; vi<this->bitmap->height(); vi++)
    {
        for(hi=0; hi<this->bitmap->width(); hi++)
        {
              if((hi!=0) && (vi!= 0))   //blok nie na krawedzi
              {
                     pw[0]=abs(qRed(bitmap->pixel(hi,vi-1))-qRed(bitmap->pixel(hi-1,vi-1)));
                     pw[1]=abs(qGreen(bitmap->pixel(hi,vi-1))-qGreen(bitmap->pixel(hi-1,vi-1)));
                     pw[2]=abs(qBlue(bitmap->pixel(hi,vi-1))-qBlue(bitmap->pixel(hi-1,vi-1)));
                     pg[0]=abs(qRed(bitmap->pixel(hi-1,vi))-qRed(bitmap->pixel(hi-1,vi-1)));
                     pg[1]=abs(qGreen(bitmap->pixel(hi-1,vi))-qGreen(bitmap->pixel(hi-1,vi-1)));
                     pg[2]=abs(qBlue(bitmap->pixel(hi-1,vi))-qBlue(bitmap->pixel(hi-1,vi-1)));
                     pgl[0]=abs(qRed(bitmap->pixel(hi,vi-1))+qRed(bitmap->pixel(hi-1,vi))-2*qRed(bitmap->pixel(hi-1,vi-1)));
                     pgl[1]=abs(qGreen(bitmap->pixel(hi,vi-1))+qGreen(bitmap->pixel(hi-1,vi))-2*qGreen(bitmap->pixel(hi-1,vi-1)));
                     pgl[2]=abs(qBlue(bitmap->pixel(hi,vi-1))+qBlue(bitmap->pixel(hi-1,vi))-2*qBlue(bitmap->pixel(hi-1,vi-1)));

                     if((pw[0]< pg[0]) && (pw[0]< pgl[0])) //pw jest najmniejsze
                     {
                         Rx=qRed(bitmap->pixel(hi,vi))-qRed(bitmap->pixel(hi-1,vi));
                     }
                     else if((pg[0]<pw[0])&&(pg[0]<pgl[0])) //pg jest najmniejsze
                     {
                         Rx=qRed(bitmap->pixel(hi,vi))-qRed(bitmap->pixel(hi,vi-1));
                     }
                     else //pgl jest najmniejsze
                     {
                         Rx=qRed(bitmap->pixel(hi,vi))-qRed(bitmap->pixel(hi-1,vi-1));
                     }

                     if((pw[1]< pg[1]) && (pw[1]< pgl[1])) //pw jest najmniejsze
                     {
                         Gx=qGreen(bitmap->pixel(hi,vi))-qGreen(bitmap->pixel(hi-1,vi));
                     }
                     else if((pg[1]<pw[1])&&(pg[1]<pgl[1])) //pg jest najmniejsze
                     {
                         Gx=qGreen(bitmap->pixel(hi,vi))-qGreen(bitmap->pixel(hi,vi-1));
                     }
                     else //pgl jest najmniejsze
                     {
                         Gx=qGreen(bitmap->pixel(hi,vi))-qGreen(bitmap->pixel(hi-1,vi-1));
                     }

                     if((pw[2]< pg[2]) && (pw[2]< pgl[2])) //pw jest najmniejsze
                     {
                         Bx=qBlue(bitmap->pixel(hi,vi))-qBlue(bitmap->pixel(hi-1,vi));
                     }
                     else if((pg[2]<pw[2])&&(pg[2]<pgl[2])) //pg jest najmniejsze
                     {
                         Bx=qBlue(bitmap->pixel(hi,vi))-qBlue(bitmap->pixel(hi,vi-1));
                     }
                     else //pgl jest najmniejsze
                     {
                         Bx=qBlue(bitmap->pixel(hi,vi))-qBlue(bitmap->pixel(hi-1,vi-1));
                     }

              }
              else if(hi!=0)
              {
                  Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi-1,vi));
                  Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi-1,vi));
                  Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi-1,vi));

                 // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi gornej";
              }
              else if(vi!=0)
              {
                  Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi,vi-1));
                  Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi,vi-1));
                  Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi,vi-1));
                  //qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi bocznej";
              }
              else   //gorny lewy piksel
              {
                  Rx=qRed(this->bitmap->pixel(hi,vi));
                  Gx=qGreen(this->bitmap->pixel(hi,vi));
                  Bx=qBlue(this->bitmap->pixel(hi,vi));
                 // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest w gornym lewym rogu";
              }
              data[(hi+vi*parameters->bitmap.width)*3] = Rx;
              data[(hi+vi*parameters->bitmap.width)*3+1] = Gx;
              data[(hi+vi*parameters->bitmap.width)*3+2] = Bx;
              /*Tutaj otrzymujesz już zakodowane dane po wyjsciu z predyktora. Teraz są to shorty:
                Rx-zakodowane z kanału Red
                Gx-zakodowane z kanału Green
                Bx-zakodowanie z kanału Blue
              */
             //qDebug()<<"x="<<hi<<" y= "<<vi<<Rx<<" "<<Gx<<" "<<Bx;
        }
    }

}

void compresor::MED_MAP(void)
{
    int vi;   //iterator pionowy pikseli
    int hi;   //iterator poziomy pikseli
    short Rx,Gx,Bx;
    QRgb g,w,gl;

    parameters->bitmap.height = bitmap->height();
    parameters->bitmap.width = bitmap->width();
    parameters->segment_count = 0;
    parameters->coefficient_count = 0;
    delete parameters->coefficients;
    parameters->coefficients = NULL;
    parameters->segment_size = 0;
    parameters->compression_type = 3;
    delete data;
    data = new short[parameters->bitmap.height*parameters->bitmap.width*3];

    for(vi=0 ; vi<this->bitmap->height(); vi++)
    {
        for(hi=0; hi<this->bitmap->width(); hi++)
        {
              if((hi!=0) && (vi!= 0))   //blok nie na krawedzi
              {

                    g=bitmap->pixel(hi,vi-1);
                    w=bitmap->pixel(hi-1,vi);
                    gl=bitmap->pixel(hi-1,vi-1);


                    //kolor czerwony
                    if(qRed(gl)>=qRed(w) &&(qRed(gl)>=qRed(g))) //gl >=max(w,g)
                    {
                        if(qRed(w)<qRed(g))
                        {
                            Rx=qRed(bitmap->pixel(hi,vi))-qRed(w);
                        }
                        else
                        {
                            Rx=qRed(bitmap->pixel(hi,vi))-qRed(g);
                        }
                    }
                    else if(qRed(gl)<=qRed(w) &&(qRed(gl)<=qRed(g))) //gl <=min(w,g
                    {
                        if(qRed(w)>qRed(g))
                        {
                            Rx=qRed(bitmap->pixel(hi,vi))-qRed(w);
                        }
                        else
                        {
                            Rx=qRed(bitmap->pixel(hi,vi))-qRed(g);
                        }
                    }
                    else
                    {
                        Rx=qRed(bitmap->pixel(hi,vi))-qRed(w)-qRed(g)+qRed(gl);
                    }


                    //kolor zielony
                    if(qGreen(gl)>=qGreen(w) &&(qGreen(gl)>=qGreen(g))) //gl >=max(w,g)
                    {
                        if(qGreen(w)<qGreen(g))
                        {
                            Gx=qGreen(bitmap->pixel(hi,vi))-qGreen(w);
                        }
                        else
                        {
                            Gx=qGreen(bitmap->pixel(hi,vi))-qGreen(g);
                        }
                    }
                    else if(qGreen(gl)<=qGreen(w) &&(qGreen(gl)<=qGreen(g))) //gl <=min(w,g
                    {
                        if(qGreen(w)>qGreen(g))
                        {
                            Gx=qGreen(bitmap->pixel(hi,vi))-qGreen(w);
                        }
                        else
                        {
                            Gx=qGreen(bitmap->pixel(hi,vi))-qGreen(g);
                        }
                    }
                    else
                    {
                        Gx=qGreen(bitmap->pixel(hi,vi))-qGreen(w)-qGreen(g)+qGreen(gl);
                    }

                    //kolor niebieski


                    if(qBlue(gl)>=qBlue(w) &&(qBlue(gl)>=qBlue(g))) //gl >=max(w,g)
                    {
                        if(qBlue(w)<qBlue(g))
                        {
                            Bx=qBlue(bitmap->pixel(hi,vi))-qBlue(w);
                        }
                        else
                        {
                            Bx=qBlue(bitmap->pixel(hi,vi))-qBlue(g);
                        }
                    }
                    else if(qBlue(gl)<=qBlue(w) &&(qBlue(gl)<=qBlue(g))) //gl <=min(w,g
                    {
                        if(qBlue(w)>qBlue(g))
                        {
                            Bx=qBlue(bitmap->pixel(hi,vi))-qBlue(w);
                        }
                        else
                        {
                            Bx=qBlue(bitmap->pixel(hi,vi))-qBlue(g);
                        }
                    }
                    else
                    {
                        Bx=qBlue(bitmap->pixel(hi,vi))-qBlue(w)-qBlue(g)+qBlue(gl);
                    }

              }
              else if(hi!=0)
              {
                  Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi-1,vi));
                  Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi-1,vi));
                  Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi-1,vi));

                 // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi gornej";
              }
              else if(vi!=0)
              {
                  Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi,vi-1));
                  Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi,vi-1));
                  Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi,vi-1));
                  //qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi bocznej";
              }
              else   //gorny lewy piksel
              {
                  Rx=qRed(this->bitmap->pixel(hi,vi));
                  Gx=qGreen(this->bitmap->pixel(hi,vi));
                  Bx=qBlue(this->bitmap->pixel(hi,vi));
                 // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest w gornym lewym rogu";
              }
              data[(hi+vi*parameters->bitmap.width)*3] = Rx;
              data[(hi+vi*parameters->bitmap.width)*3+1] = Gx;
              data[(hi+vi*parameters->bitmap.width)*3+2] = Bx;
              /*Tutaj otrzymujesz już zakodowane dane po wyjsciu z predyktora. Teraz są to shorty:
                Rx-zakodowane z kanału Red
                Gx-zakodowane z kanału Green
                Bx-zakodowanie z kanału Blue
              */
             //qDebug()<<"x="<<hi<<" y= "<<vi<<Rx<<" "<<Gx<<" "<<Bx;
          }
    }
}

void compresor::Graham(void)
{
    int vi;   //iterator pionowy pikseli
    int hi;   //iterator poziomy pikseli
    short Rx,Gx,Bx;

    parameters->bitmap.height = bitmap->height();
    parameters->bitmap.width = bitmap->width();
    parameters->segment_count = 0;
    parameters->coefficient_count = 0;
    delete parameters->coefficients;
    parameters->coefficients = NULL;
    parameters->segment_size = 0;
    parameters->compression_type = 4;
    delete data;
    data = new short[parameters->bitmap.height*parameters->bitmap.width*3];

    for(vi=0 ; vi<this->bitmap->height(); vi++)
    {
        for(hi=0; hi<this->bitmap->width(); hi++)
        {
              if((hi!=0) && (vi!= 0))   //blok nie na krawedzi
              {
                  //czerwony
                   if(abs(qRed(bitmap->pixel(hi-1,vi-1))-qRed(bitmap->pixel(hi,vi-1)) > abs(qRed(bitmap->pixel(hi-1,vi-1))-qRed(bitmap->pixel(hi-1,vi)))))
                   {
                       Rx=qRed(bitmap->pixel(hi,vi))-qRed(bitmap->pixel(hi,vi-1));
                   }
                   else
                   {
                       Rx=qRed(bitmap->pixel(hi,vi))-qRed(bitmap->pixel(hi-1,vi));
                   }
                   //zielony
                   if(abs(qGreen(bitmap->pixel(hi-1,vi-1))-qGreen(bitmap->pixel(hi,vi-1)) > abs(qGreen(bitmap->pixel(hi-1,vi-1))-qGreen(bitmap->pixel(hi-1,vi)))))
                   {
                       Gx=qGreen(bitmap->pixel(hi,vi))-qGreen(bitmap->pixel(hi,vi-1));
                   }
                   else
                   {
                       Gx=qGreen(bitmap->pixel(hi,vi))-qGreen(bitmap->pixel(hi-1,vi));
                   }
                   //niebieski
                   if(abs(qBlue(bitmap->pixel(hi-1,vi-1))-qBlue(bitmap->pixel(hi,vi-1)) > abs(qBlue(bitmap->pixel(hi-1,vi-1))-qBlue(bitmap->pixel(hi-1,vi)))))
                   {
                       Bx=qBlue(bitmap->pixel(hi,vi))-qBlue(bitmap->pixel(hi,vi-1));
                   }
                   else
                   {
                       Bx=qBlue(bitmap->pixel(hi,vi))-qBlue(bitmap->pixel(hi-1,vi));
                   }
              }
              else if(hi!=0)
              {
                  Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi-1,vi));
                  Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi-1,vi));
                  Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi-1,vi));

                 // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi gornej";
              }
              else if(vi!=0)
              {
                  Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi,vi-1));
                  Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi,vi-1));
                  Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi,vi-1));
                  //qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi bocznej";
              }
              else   //gorny lewy piksel
              {
                  Rx=qRed(this->bitmap->pixel(hi,vi));
                  Gx=qGreen(this->bitmap->pixel(hi,vi));
                  Bx=qBlue(this->bitmap->pixel(hi,vi));
                 // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest w gornym lewym rogu";
              }
              data[(hi+vi*parameters->bitmap.width)*3] = Rx;
              data[(hi+vi*parameters->bitmap.width)*3+1] = Gx;
              data[(hi+vi*parameters->bitmap.width)*3+2] = Bx;
              /*Tutaj otrzymujesz już zakodowane dane po wyjsciu z predyktora. Teraz są to shorty:
                Rx-zakodowane z kanału Red
                Gx-zakodowane z kanału Green
                Bx-zakodowanie z kanału Blue
              */
             //qDebug()<<"x="<<hi<<" y= "<<vi<<Rx<<" "<<Gx<<" "<<Bx;
        }
    }
}

void compresor::DARC(void)
{
    int vi;   //iterator pionowy pikseli
    int hi;   //iterator poziomy pikseli
    short Rx,Gx,Bx;
    float alfa;
    float gv,gh;
    QRgb g,w,gl;

    parameters->bitmap.height = bitmap->height();
    parameters->bitmap.width = bitmap->width();
    parameters->segment_count = 0;
    parameters->coefficient_count = 0;
    delete parameters->coefficients;
    parameters->coefficients = NULL;
    parameters->segment_size = 0;
    parameters->compression_type = 5;
    delete data;
    data = new short[parameters->bitmap.height*parameters->bitmap.width*3];

    for(vi=0 ; vi<this->bitmap->height(); vi++)
    {
        for(hi=0; hi<this->bitmap->width(); hi++)
        {
              if((hi!=0) && (vi!= 0))   //blok nie na krawedzi
              {
                  g=bitmap->pixel(hi,vi-1);
                  w=bitmap->pixel(hi-1,vi);
                  gl=bitmap->pixel(hi-1,vi-1);

                  //czerwony
                  gh=abs(qRed(gl)-qRed(g));
                  gv=abs(qRed(gl)-qRed(w));
                  if(gh+gv!=0)
                  {
                    alfa=gv/(gh+gv);
                  }
                  else
                  {
                      alfa=0.5;
                  }

                  Rx=qRed(bitmap->pixel(hi,vi))-(int)(alfa*qRed(gl)+(1-alfa)*qRed(g));
                   //qDebug()<<"x="<<hi<<" y= "<<vi<<" oryginalnie czerwony= "<< qRed(bitmap->pixel(hi,vi)) <<" wyjscie predyktora " << Rx <<" Poprawka="<<(int)(alfa*qRed(gl)+(1-alfa)*qRed(g));

                  //zielony
                  gh=abs(qGreen(gl)-qGreen(g));
                  gv=abs(qGreen(gl)-qGreen(w));

                  if(gh+gv!=0)
                  {
                    alfa=gv/(gh+gv);
                  }
                  else
                  {
                      alfa=0.5;
                  }
                  Gx=qGreen(bitmap->pixel(hi,vi))-(int)(alfa*qGreen(gl)+(1-alfa)*qGreen(g));

                  //niebieski
                  gh=abs(qBlue(gl)-qBlue(g));
                  gv=abs(qBlue(gl)-qBlue(w));
                  if(gh+gv!=0)
                  {
                    alfa=gv/(gh+gv);
                  }
                  else
                  {
                      alfa=0.5;
                  }
                  Bx=qBlue(bitmap->pixel(hi,vi))-(int)(alfa*qBlue(gl)+(1-alfa)*qBlue(g));
//                  qDebug()<<"x="<<hi<<" y= "<<vi<<Rx<<" "<<Gx<<" "<<Bx<<"oryginalny pixel R="<<qRed(this->bitmap->pixel(hi,vi))<<" G="<<qGreen(this->bitmap->pixel(hi,vi))<<" B= "<<qBlue(this->bitmap->pixel(hi,vi));

              }
              else if(hi!=0)
              {
                  Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi-1,vi));
                  Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi-1,vi));
                  Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi-1,vi));

                 // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi gornej";
              }
              else if(vi!=0)
              {
                  Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi,vi-1));
                  Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi,vi-1));
                  Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi,vi-1));
                  //qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi bocznej";
              }
              else   //gorny lewy piksel
              {
                  Rx=qRed(this->bitmap->pixel(hi,vi));
                  Gx=qGreen(this->bitmap->pixel(hi,vi));
                  Bx=qBlue(this->bitmap->pixel(hi,vi));
                 // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest w gornym lewym rogu";
              }
              data[(hi+vi*parameters->bitmap.width)*3] = Rx;
              data[(hi+vi*parameters->bitmap.width)*3+1] = Gx;
              data[(hi+vi*parameters->bitmap.width)*3+2] = Bx;
              /*Tutaj otrzymujesz już zakodowane dane po wyjsciu z predyktora. Teraz są to shorty:
                Rx-zakodowane z kanału Red
                Gx-zakodowane z kanału Green
                Bx-zakodowanie z kanału Blue
              */

        }
    }

}

void compresor::ALCM(int size)
{
    int v=0;  //iterator pionowy blokow
    int h=0;  //iterator poziomy blokow
    int vi;   //iterator pionowy wewnatrz bloku
    int hi;   //iterator poziomy wewnatrz bloku
    int a;
    int data_iter=0;
    short Rx,Gx,Bx;
    float Wgl[3],Wg[3],Wgp[3],Ww[3],Www[3];

    parameters->bitmap.height = bitmap->height();
    parameters->bitmap.width = bitmap->width();
    parameters->segment_count = 0;
    parameters->coefficient_count = 0;
    delete parameters->coefficients;
    parameters->coefficients = NULL;
    parameters->segment_size = size;
    parameters->compression_type = 6;
    delete data;
    data = new short[parameters->bitmap.height*parameters->bitmap.width*3];

    for(v=0; v < this->bitmap->height(); v+=size)
    {
        for(h=0; h<this->bitmap->width() ; h+=size)
        {

              for(int i=0; i<3; i++)
              {
                 Wgl[i]=Wg[i]=Wgp[i]=Ww[i]=Www[i]=1.0/5.0;  //zerowanie wspolczynnikow

              }

              for(vi=v ; (vi<v+size) && (vi < bitmap->height()); vi++)
              {
                  for(hi=h; (hi<h+size) && (hi < bitmap->width()); hi++)
                  {
                        if((hi!=0) && (hi!=1) && (hi!=this->bitmap->width()-1) && (vi!= 0))   //blok nie na krawedzi
                        {
                            if(hi!=(h+size-1))
                            {
                            Rx=qRed(this->bitmap->pixel(hi,vi))-(int)(Wgl[0]*qRed(this->bitmap->pixel(hi-1,vi-1))+
                                                                 Wg[0]*qRed(this->bitmap->pixel(hi,vi-1))+
                                                                 Wgp[0]*qRed(this->bitmap->pixel(hi+1,vi-1))+
                                                                 Ww[0]*qRed(this->bitmap->pixel(hi-1,vi))+
                                                                 Www[0]*qRed(this->bitmap->pixel(hi-2,vi)));

                            Gx=qGreen(this->bitmap->pixel(hi,vi))-(int)(Wgl[0]*qGreen(this->bitmap->pixel(hi-1,vi-1))+
                                                                 Wg[0]*qGreen(this->bitmap->pixel(hi,vi-1))+
                                                                 Wgp[0]*qGreen(this->bitmap->pixel(hi+1,vi-1))+
                                                                 Ww[0]*qGreen(this->bitmap->pixel(hi-1,vi))+
                                                                 Www[0]*qGreen(this->bitmap->pixel(hi-2,vi)));
                            Bx=qBlue(this->bitmap->pixel(hi,vi))-(int)(Wgl[0]*qBlue(this->bitmap->pixel(hi-1,vi-1))+
                                                                 Wg[0]*qBlue(this->bitmap->pixel(hi,vi-1))+
                                                                 Wgp[0]*qBlue(this->bitmap->pixel(hi+1,vi-1))+
                                                                 Ww[0]*qBlue(this->bitmap->pixel(hi-1,vi))+
                                                                 Www[0]*qBlue(this->bitmap->pixel(hi-2,vi)));
                            }
                            else
                            {
                                Rx=qRed(this->bitmap->pixel(hi,vi))-(int)(Wgl[0]*qRed(this->bitmap->pixel(hi-1,vi-1))+
                                                                     Wg[0]*qRed(this->bitmap->pixel(hi,vi-1))+
                                                                     Ww[0]*qRed(this->bitmap->pixel(hi-1,vi))+
                                                                     Www[0]*qRed(this->bitmap->pixel(hi-2,vi)));

                                Gx=qGreen(this->bitmap->pixel(hi,vi))-(int)(Wgl[0]*qGreen(this->bitmap->pixel(hi-1,vi-1))+
                                                                     Wg[0]*qGreen(this->bitmap->pixel(hi,vi-1))+
                                                                     Ww[0]*qGreen(this->bitmap->pixel(hi-1,vi))+
                                                                     Www[0]*qGreen(this->bitmap->pixel(hi-2,vi)));
                                Bx=qBlue(this->bitmap->pixel(hi,vi))-(int)(Wgl[0]*qBlue(this->bitmap->pixel(hi-1,vi-1))+
                                                                     Wg[0]*qBlue(this->bitmap->pixel(hi,vi-1))+
                                                                     Ww[0]*qBlue(this->bitmap->pixel(hi-1,vi))+
                                                                     Www[0]*qBlue(this->bitmap->pixel(hi-2,vi)));
                            }
                            //modyfikacja wspolczynnikow

                            for(int i=0; i<3; i++)
                            {
                                a=1;
                                if((Rx<0) && (i==0))
                                {
                                    a=-1;
                                }
                                else if((Gx<0)&&(i==1))
                                {
                                    a=-1;
                                }
                                else if((Bx<0)&&(i==2))
                                {
                                    a=-1;
                                }

                                //ustalanie najwiekszego zgodnie z priorytetami
                                if((Wgl[i]>Wg[i])&&(Wgl[i]>Wgp[i])&&(Wgl[i]>Ww[i])&&(Wgl[i]>Www[i]))//Wgl najwiekszy
                                {
                                    Wgl[i]+=a*1.0/256.0;
                                }
                                else if((Wg[i]>Wgl[i])&&(Wg[i]>Wgp[i])&&(Wg[i]>Ww[i])&&(Wg[i]>Www[i]))
                                {
                                    Wg[i]+=a*1.0/256.0;
                                }
                                else if((Wgp[i]>Wgl[i])&&(Wgp[i]>Wg[i])&&(Wgp[i]>Ww[i])&&(Wgp[i]>Www[i]))
                                {
                                    Wgp[i]+=a*1.0/256.0;
                                }
                                else if((Ww[i]>Wgl[i])&&(Ww[i]>Wg[i])&&(Ww[i]>Wgp[i])&&(Ww[i]>Www[i]))
                                {
                                    Ww[i]+=a*1.0/256.0;
                                }
                                else if((Www[i]>Wgl[i])&&(Www[i]>Wg[i])&&(Www[i]>Wgp[i])&&(Www[i]>Ww[i]))
                                {
                                    Www[i]+=a*1.0/256.0;
                                }
                                //ustalanie najmniejszego zgodnie z priorytetami

                                if((Wgl[i]<Wg[i])&&(Wgl[i]<Wgp[i])&&(Wgl[i]<Ww[i])&&(Wgl[i]<Www[i]))//Wgl najmniejszy
                                {
                                    Wgl[i]-=a*1.0/256.0;
                                }
                                else if((Wg[i]<Wgl[i])&&(Wg[i]<Wgp[i])&&(Wg[i]<Ww[i])&&(Wg[i]<Www[i]))
                                {
                                    Wg[i]-=a*1.0/256.0;
                                }
                                else if((Wgp[i]<Wgl[i])&&(Wgp[i]<Wg[i])&&(Wgp[i]<Ww[i])&&(Wgp[i]<Www[i]))
                                {
                                    Wgp[i]-=a*1.0/256.0;
                                }
                                else if((Ww[i]<Wgl[i])&&(Ww[i]<Wg[i])&&(Ww[i]<Wgp[i])&&(Ww[i]<Www[i]))
                                {
                                    Ww[i]-=a*1.0/256.0;
                                }
                                else if((Www[i]<Wgl[i])&&(Www[i]<Wg[i])&&(Www[i]<Wgp[i])&&(Www[i]<Ww[i]))
                                {
                                    Www[i]-=a*1.0/256.0;
                                }

                            }

                        }
                        else if((hi==0) && (vi==0)) //gorny lewy przesyłany bezpośrednio
                        {
                            Rx=qRed(this->bitmap->pixel(hi,vi));
                            Gx=qGreen(this->bitmap->pixel(hi,vi));
                            Bx=qBlue(this->bitmap->pixel(hi,vi));
                        }

                        else if(vi==0) //krawedz górna
                        {
                            Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi-1,vi));
                            Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi-1,vi));
                            Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi-1,vi));
                            //qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi bocznej";
                        }
                        else if(hi==0)  //krawedz skrajnie lewa
                        {
                            Rx=qRed(this->bitmap->pixel(hi,vi))-qRed(this->bitmap->pixel(hi,vi-1));
                            Gx=qGreen(this->bitmap->pixel(hi,vi))-qGreen(this->bitmap->pixel(hi,vi-1));
                            Bx=qBlue(this->bitmap->pixel(hi,vi))-qBlue(this->bitmap->pixel(hi,vi-1));

                           // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi gornej";
                        }
                        else if(hi==1)  //krawedz druga od lewej
                        {
                            Rx=qRed(this->bitmap->pixel(hi,vi))-0.5*qRed(this->bitmap->pixel(hi-1,vi))-0.5*qRed(this->bitmap->pixel(hi,vi-1));
                            Gx=qGreen(this->bitmap->pixel(hi,vi))-0.5*qGreen(this->bitmap->pixel(hi-1,vi))-0.5*qGreen(this->bitmap->pixel(hi,vi-1));
                            Bx=qBlue(this->bitmap->pixel(hi,vi))-0.5*qBlue(this->bitmap->pixel(hi-1,vi))-0.5*qBlue(this->bitmap->pixel(hi,vi-1));

                           // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi gornej";
                        }
                        else if(hi==bitmap->width()-1)  //skrajnie prawa
                        {
                            Rx=qRed(this->bitmap->pixel(hi,vi))-0.5*qRed(this->bitmap->pixel(hi-1,vi))-0.5*qRed(this->bitmap->pixel(hi,vi-1));
                            Gx=qGreen(this->bitmap->pixel(hi,vi))-0.5*qGreen(this->bitmap->pixel(hi-1,vi))-0.5*qGreen(this->bitmap->pixel(hi,vi-1));
                            Bx=qBlue(this->bitmap->pixel(hi,vi))-0.5*qBlue(this->bitmap->pixel(hi-1,vi))-0.5*qBlue(this->bitmap->pixel(hi,vi-1));

                           // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi gornej";
                        }

//                        data[(hi+vi*parameters->bitmap.width)*3] = Rx;
//                        data[(hi+vi*parameters->bitmap.width)*3+1] = Gx;
//                        data[(hi+vi*parameters->bitmap.width)*3+2] = Bx;
                        /*Tutaj otrzymujesz już zakodowane dane po wyjsciu z predyktora. Teraz są to shorty:
                          Rx-zakodowane z kanału Red
                          Gx-zakodowane z kanału Green
                          Bx-zakodowanie z kanału Blue
                        */
                        data[data_iter]=Rx;
                        data_iter++;
                        data[data_iter]=Gx;
                        data_iter++;
                        data[data_iter]=Bx;
                        data_iter++;
                  }
              }
        }
    }
}

void compresor::de_compresor_forward3(predictor_header *ph)
{
    int v=0;  //iterator pionowy blokow
    int h=0;  //iterator poziomy blokow
    int vi;   //iterator pionowy wewnatrz bloku
    int hi;   //iterator poziomy wewnatrz bloku
    int data_iter=0;
    int coef_iter=0;
    short Rx,Gx,Bx;
    unsigned short size=ph->segment_size;
    float Alfa0[3],Alfa1[3],Alfa2[3];
    debitmap = new QImage(ph->bitmap.width,ph->bitmap.height, QImage::Format_RGB32);
    int height=debitmap->height();
    int width=debitmap->width();


    for(v=0; v < debitmap->height(); v+=size)
    {
        for(h=0; h<debitmap->width() ; h+=size)
        {
                 for(int i=0; i<3; i++)
                 {
                  Alfa0[i]=this->short_to_float(ph->coefficients[coef_iter]);
                  coef_iter++;
                  Alfa1[i]=this->short_to_float(ph->coefficients[coef_iter]);
                  coef_iter++;
                  Alfa2[i]=this->short_to_float(ph->coefficients[coef_iter]);
                  coef_iter++;
                 }

              //dekodowanie bloku danych

              for(vi=v ; (vi<v+size) && (vi < height); vi++)
              {
                  for(hi=h; (hi<h+size) && (hi < width); hi++)
                  {
                        if((hi!=0) && (vi!= 0))   //blok nie na krawedzi
                        {
                               Rx=dedata[data_iter]+(int)(Alfa0[0]*qRed(debitmap->pixel(hi-1,vi))+Alfa1[0]*qRed(debitmap->pixel(hi,vi-1))+Alfa2[0]*qRed(debitmap->pixel(hi-1,vi-1)));
                               data_iter++;
                               Gx=dedata[data_iter]+(int)(Alfa0[1]*qGreen(debitmap->pixel(hi-1,vi))+Alfa1[1]*qGreen(debitmap->pixel(hi,vi-1))+Alfa2[1]*qGreen(debitmap->pixel(hi-1,vi-1)));
                               data_iter++;
                               Bx=dedata[data_iter]+(int)(Alfa0[2]*qBlue(debitmap->pixel(hi-1,vi))+Alfa1[2]*qBlue(debitmap->pixel(hi,vi-1))+Alfa2[2]*qBlue(debitmap->pixel(hi-1,vi-1)));
                               data_iter++;
                               debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));

                        }
                        else if(hi!=0)  //krawedź gorna
                        {
                            Rx=dedata[data_iter]+qRed(debitmap->pixel(hi-1,vi));
                            data_iter++;
                            Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi-1,vi));
                            data_iter++;
                            Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi-1,vi));
                            data_iter++;
                            debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));




                        }
                        else if(vi!=0)
                        {
                            Rx=dedata[data_iter]+qRed(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));

                        }
                        else   //gorny lewy piksel
                        {
                            Rx=dedata[data_iter];
                            data_iter++;
                            Gx=dedata[data_iter];
                            data_iter++;
                            Bx=dedata[data_iter];
                            data_iter++;
                            debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));

                        }

                  }
              }
        }
    }

}

void compresor::de_compresor_forward2(predictor_header *ph)
{
    int v=0;  //iterator pionowy blokow
    int h=0;  //iterator poziomy blokow
    int vi;   //iterator pionowy wewnatrz bloku
    int hi;   //iterator poziomy wewnatrz bloku
    int data_iter=0;
    int coef_iter=0;
    short Rx,Gx,Bx;
    unsigned short size=ph->segment_size;
    float Alfa0[3],Alfa1[3];
    debitmap = new QImage(ph->bitmap.width,ph->bitmap.height, QImage::Format_RGB32);


    for(v=0; v < debitmap->height(); v+=size)
    {
        for(h=0; h<debitmap->width() ; h+=size)
        {
                 for(int i=0; i<3; i++)
                 {
                  Alfa0[i]=this->short_to_float(ph->coefficients[coef_iter]);
                 // qDebug()<<"alfa0["<<i<<"] dla h="<<h<<" v="<<v<<"w dekoderze wynosi" <<Alfa0[i];
                  coef_iter++;
                  Alfa1[i]=this->short_to_float(ph->coefficients[coef_iter]);
                  coef_iter++;
                 }

              //dekodowanie bloku danych

              for(vi=v ; (vi<v+size) && (vi < debitmap->height()); vi++)
              {
                  for(hi=h; (hi<h+size) && (hi< debitmap->width()); hi++)
                  {
                        if((hi!=0) && (vi!= 0))   //blok nie na krawedzi
                        {
                               Rx=dedata[data_iter]+(int)(Alfa0[0]*qRed(debitmap->pixel(hi-1,vi))+Alfa1[0]*qRed(debitmap->pixel(hi,vi-1)));
                               data_iter++;
                               Gx=dedata[data_iter]+(int)(Alfa0[1]*qGreen(debitmap->pixel(hi-1,vi))+Alfa1[1]*qGreen(debitmap->pixel(hi,vi-1)));
                               data_iter++;
                               Bx=dedata[data_iter]+(int)(Alfa0[2]*qBlue(debitmap->pixel(hi-1,vi))+Alfa1[2]*qBlue(debitmap->pixel(hi,vi-1)));
                               data_iter++;
                               debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));


                        }
                        else if(hi!=0)  //krawedź gorna
                        {
                            Rx=dedata[data_iter]+qRed(debitmap->pixel(hi-1,vi));
                            data_iter++;
                            Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi-1,vi));
                            data_iter++;
                            Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi-1,vi));
                            data_iter++;
                            debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));



                        }
                        else if(vi!=0)
                        {
                            Rx=dedata[data_iter]+qRed(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));


                        }
                        else   //gorny lewy piksel
                        {
                            Rx=dedata[data_iter];
                            data_iter++;
                            Gx=dedata[data_iter];
                            data_iter++;
                            Bx=dedata[data_iter];
                            data_iter++;
                            debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));

                        }

                  }
              }
        }
    }
}

void compresor::de_PNGm5(predictor_header *ph)
{

    int vi;   //iterator pionowy pikseli
    int hi;   //iterator poziomy pikseli
    short pw[3],pg[3],pgl[3];
    short Rx,Gx,Bx;
    int data_iter=0;

    debitmap = new QImage(ph->bitmap.width,ph->bitmap.height, QImage::Format_RGB32);
    for(vi=0 ; vi<debitmap->height(); vi++)
    {
        for(hi=0; hi<debitmap->width(); hi++)
        {
              if((hi!=0) && (vi!= 0))   //blok nie na krawedzi
              {
                     pw[0]=abs(qRed(debitmap->pixel(hi,vi-1))-qRed(debitmap->pixel(hi-1,vi-1)));
                     pw[1]=abs(qGreen(debitmap->pixel(hi,vi-1))-qGreen(debitmap->pixel(hi-1,vi-1)));
                     pw[2]=abs(qBlue(debitmap->pixel(hi,vi-1))-qBlue(debitmap->pixel(hi-1,vi-1)));
                     pg[0]=abs(qRed(debitmap->pixel(hi-1,vi))-qRed(debitmap->pixel(hi-1,vi-1)));
                     pg[1]=abs(qGreen(debitmap->pixel(hi-1,vi))-qGreen(debitmap->pixel(hi-1,vi-1)));
                     pg[2]=abs(qBlue(debitmap->pixel(hi-1,vi))-qBlue(debitmap->pixel(hi-1,vi-1)));
                     pgl[0]=abs(qRed(debitmap->pixel(hi,vi-1))+qRed(debitmap->pixel(hi-1,vi))-2*qRed(debitmap->pixel(hi-1,vi-1)));
                     pgl[1]=abs(qGreen(debitmap->pixel(hi,vi-1))+qGreen(debitmap->pixel(hi-1,vi))-2*qGreen(debitmap->pixel(hi-1,vi-1)));
                     pgl[2]=abs(qBlue(debitmap->pixel(hi,vi-1))+qBlue(debitmap->pixel(hi-1,vi))-2*qBlue(debitmap->pixel(hi-1,vi-1)));

                     if((pw[0]< pg[0]) && (pw[0]< pgl[0])) //pw jest najmniejsze
                     {
                         Rx=dedata[data_iter]+qRed(debitmap->pixel(hi-1,vi));
                         data_iter++;
                     }
                     else if((pg[0]<pw[0])&&(pg[0]<pgl[0])) //pg jest najmniejsze
                     {
                         Rx=dedata[data_iter]+qRed(debitmap->pixel(hi,vi-1));
                         data_iter++;
                     }
                     else //pgl jest najmniejsze
                     {
                         Rx=dedata[data_iter]+qRed(debitmap->pixel(hi-1,vi-1));
                         data_iter++;
                     }

                     if((pw[1]< pg[1]) && (pw[1]< pgl[1])) //pw jest najmniejsze
                     {
                         Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi-1,vi));
                         data_iter++;
                     }
                     else if((pg[1]<pw[1])&&(pg[1]<pgl[1])) //pg jest najmniejsze
                     {
                         Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi,vi-1));
                         data_iter++;
                     }
                     else //pgl jest najmniejsze
                     {
                         Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi-1,vi-1));
                         data_iter++;
                     }

                     if((pw[2]< pg[2]) && (pw[2]< pgl[2])) //pw jest najmniejsze
                     {
                         Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi-1,vi));
                         data_iter++;
                     }
                     else if((pg[2]<pw[2])&&(pg[2]<pgl[2])) //pg jest najmniejsze
                     {
                         Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi,vi-1));
                         data_iter++;
                     }
                     else //pgl jest najmniejsze
                     {
                         Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi-1,vi-1));
                         data_iter++;
                     }
                     debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));


              }
              else if(hi!=0)  //krawedź gorna
              {
                  Rx=dedata[data_iter]+qRed(debitmap->pixel(hi-1,vi));
                  data_iter++;
                  Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi-1,vi));
                  data_iter++;
                  Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi-1,vi));
                  data_iter++;
                  debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));



              }
              else if(vi!=0)
              {
                  Rx=dedata[data_iter]+qRed(debitmap->pixel(hi,vi-1));
                  data_iter++;
                  Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi,vi-1));
                  data_iter++;
                  Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi,vi-1));
                  data_iter++;
                  debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));

              }
              else   //gorny lewy piksel
              {
                  Rx=dedata[data_iter];
                  data_iter++;
                  Gx=dedata[data_iter];
                  data_iter++;
                  Bx=dedata[data_iter];
                  data_iter++;
                  debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));
              }
              /*Tutaj otrzymujesz już zakodowane dane po wyjsciu z predyktora. Teraz są to shorty:
                Rx-zakodowane z kanału Red
                Gx-zakodowane z kanału Green
                Bx-zakodowanie z kanału Blue
              */
             //qDebug()<<"x="<<hi<<" y= "<<vi<<Rx<<" "<<Gx<<" "<<Bx;
        }
    }

}

void compresor::de_MED_MAP(predictor_header *ph)
{
    int vi;   //iterator pionowy pikseli
    int hi;   //iterator poziomy pikseli
    short Rx,Gx,Bx;
    QRgb g,w,gl;

    int data_iter=0;

    debitmap = new QImage(ph->bitmap.width,ph->bitmap.height, QImage::Format_RGB32);
    for(vi=0 ; vi<debitmap->height(); vi++)
    {
        for(hi=0; hi<debitmap->width(); hi++)
        {
              if((hi!=0) && (vi!= 0))   //blok nie na krawedzi
              {

                    g=debitmap->pixel(hi,vi-1);
                    w=debitmap->pixel(hi-1,vi);
                    gl=debitmap->pixel(hi-1,vi-1);


                    //kolor czerwony
                    if(qRed(gl)>=qRed(w) &&(qRed(gl)>=qRed(g))) //gl >=max(w,g)
                    {
                        if(qRed(w)<qRed(g))
                        {
                            Rx=dedata[data_iter]+qRed(w);
                            data_iter++;
                        }
                        else
                        {
                            Rx=dedata[data_iter]+qRed(g);
                            data_iter++;
                        }
                    }
                    else if(qRed(gl)<=qRed(w) &&(qRed(gl)<=qRed(g))) //gl <=min(w,g
                    {
                        if(qRed(w)>qRed(g))
                        {
                            Rx=dedata[data_iter]+qRed(w);
                            data_iter++;
                        }
                        else
                        {
                            Rx=dedata[data_iter]+qRed(g);
                            data_iter++;
                        }
                    }
                    else
                    {
                        Rx=dedata[data_iter]+qRed(w)+qRed(g)-qRed(gl);
                        data_iter++;
                    }


                    //kolor zielony
                    if(qGreen(gl)>=qGreen(w) &&(qGreen(gl)>=qGreen(g))) //gl >=max(w,g)
                    {
                        if(qGreen(w)<qGreen(g))
                        {
                            Gx=dedata[data_iter]+qGreen(w);
                            data_iter++;
                        }
                        else
                        {
                            Gx=dedata[data_iter]+qGreen(g);
                            data_iter++;
                        }
                    }
                    else if(qGreen(gl)<=qGreen(w) &&(qGreen(gl)<=qGreen(g))) //gl <=min(w,g
                    {
                        if(qGreen(w)>qGreen(g))
                        {
                            Gx=dedata[data_iter]+qGreen(w);
                            data_iter++;
                        }
                        else
                        {
                            Gx=dedata[data_iter]+qGreen(g);
                            data_iter++;
                        }
                    }
                    else
                    {
                        Gx=dedata[data_iter]+qGreen(w)+qGreen(g)-qGreen(gl);
                        data_iter++;
                    }

                    //kolor niebieski


                    if(qBlue(gl)>=qBlue(w) &&(qBlue(gl)>=qBlue(g))) //gl >=max(w,g)
                    {
                        if(qBlue(w)<qBlue(g))
                        {
                            Bx=dedata[data_iter]+qBlue(w);
                            data_iter++;
                        }
                        else
                        {
                            Bx=dedata[data_iter]+qBlue(g);
                            data_iter++;
                        }
                    }
                    else if(qBlue(gl)<=qBlue(w) &&(qBlue(gl)<=qBlue(g))) //gl <=min(w,g
                    {
                        if(qBlue(w)>qBlue(g))
                        {
                            Bx=dedata[data_iter]+qBlue(w);
                            data_iter++;
                        }
                        else
                        {
                            Bx=data[data_iter]+qBlue(g);
                            data_iter++;
                        }
                    }
                    else
                    {
                        Bx=dedata[data_iter]+qBlue(w)+qBlue(g)-qBlue(gl);
                        data_iter++;
                    }
                    debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));

              }
              else if(hi!=0)  //krawedź gorna
              {
                  Rx=dedata[data_iter]+qRed(debitmap->pixel(hi-1,vi));
                  data_iter++;
                  Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi-1,vi));
                  data_iter++;
                  Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi-1,vi));
                  data_iter++;
                  debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));



              }
              else if(vi!=0)
              {
                  Rx=dedata[data_iter]+qRed(debitmap->pixel(hi,vi-1));
                  data_iter++;
                  Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi,vi-1));
                  data_iter++;
                  Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi,vi-1));
                  data_iter++;
                  debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));

              }
              else   //gorny lewy piksel
              {
                  Rx=dedata[data_iter];
                  data_iter++;
                  Gx=dedata[data_iter];
                  data_iter++;
                  Bx=dedata[data_iter];
                  data_iter++;
                  debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));
              }

          }
    }
}

void compresor::de_Graham(predictor_header *ph)
{
    int vi;   //iterator pionowy pikseli
    int hi;   //iterator poziomy pikseli
    int data_iter=0;
    debitmap = new QImage(ph->bitmap.width,ph->bitmap.height, QImage::Format_RGB32);
    short Rx,Gx,Bx;
    for(vi=0 ; vi<debitmap->height(); vi++)
    {
        for(hi=0; hi<debitmap->width(); hi++)
        {
              if((hi!=0) && (vi!= 0))   //blok nie na krawedzi
              {
                  //czerwony
                   if(abs(qRed(debitmap->pixel(hi-1,vi-1))-qRed(debitmap->pixel(hi,vi-1)) > abs(qRed(debitmap->pixel(hi-1,vi-1))-qRed(debitmap->pixel(hi-1,vi)))))
                   {
                       Rx=dedata[data_iter]+qRed(debitmap->pixel(hi,vi-1));
                       data_iter++;
                   }
                   else
                   {
                       Rx=dedata[data_iter]+qRed(debitmap->pixel(hi-1,vi));
                       data_iter++;
                   }
                   //zielony
                   if(abs(qGreen(debitmap->pixel(hi-1,vi-1))-qGreen(debitmap->pixel(hi,vi-1)) > abs(qGreen(debitmap->pixel(hi-1,vi-1))-qGreen(debitmap->pixel(hi-1,vi)))))
                   {
                       Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi,vi-1));
                       data_iter++;
                   }
                   else
                   {
                       Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi-1,vi));
                       data_iter++;
                   }
                   //niebieski
                   if(abs(qBlue(debitmap->pixel(hi-1,vi-1))-qBlue(debitmap->pixel(hi,vi-1)) > abs(qBlue(debitmap->pixel(hi-1,vi-1))-qBlue(debitmap->pixel(hi-1,vi)))))
                   {
                       Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi,vi-1));
                       data_iter++;
                   }
                   else
                   {
                       Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi-1,vi));
                       data_iter++;
                   }
                   debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));
              }
              else if(hi!=0)  //krawedź gorna
              {
                  Rx=dedata[data_iter]+qRed(debitmap->pixel(hi-1,vi));
                  data_iter++;
                  Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi-1,vi));
                  data_iter++;
                  Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi-1,vi));
                  data_iter++;
                  debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));



              }
              else if(vi!=0)
              {
                  Rx=dedata[data_iter]+qRed(debitmap->pixel(hi,vi-1));
                  data_iter++;
                  Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi,vi-1));
                  data_iter++;
                  Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi,vi-1));
                  data_iter++;
                  debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));

              }
              else   //gorny lewy piksel
              {
                  Rx=dedata[data_iter];
                  data_iter++;
                  Gx=dedata[data_iter];
                  data_iter++;
                  Bx=dedata[data_iter];
                  data_iter++;
                  debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));
              }

        }
    }
}

void compresor::de_DARC(predictor_header *ph)
{
    int vi;   //iterator pionowy pikseli
    int hi;   //iterator poziomy pikseli
    short Rx,Gx,Bx;
    float alfa;
    float gv,gh;
    QRgb g,w,gl;
    int data_iter=0;
    debitmap = new QImage(ph->bitmap.width,ph->bitmap.height, QImage::Format_RGB32);

    for(vi=0 ; vi<debitmap->height(); vi++)
    {
        for(hi=0; hi<debitmap->width(); hi++)
        {
              if((hi!=0) && (vi!= 0))   //blok nie na krawedzi
              {
                  g=debitmap->pixel(hi,vi-1);
                  w=debitmap->pixel(hi-1,vi);
                  gl=debitmap->pixel(hi-1,vi-1);

                  //czerwony
                  gh=abs(qRed(gl)-qRed(g));
                  gv=abs(qRed(gl)-qRed(w));
                  if(gh+gv!=0)
                  {
                    alfa=gv/(gh+gv);
                  }
                  else
                  {
                      alfa=0.5;
                  }

                  Rx=dedata[data_iter]+int(alfa*qRed(gl)+(1-alfa)*qRed(g));
                  data_iter++;

                  //zielony
                  gh=abs(qGreen(gl)-qGreen(g));
                  gv=abs(qGreen(gl)-qGreen(w));

                  if(gh+gv!=0)
                  {
                    alfa=gv/(gh+gv);
                  }
                  else
                  {
                      alfa=0.5;
                  }
                  Gx=dedata[data_iter]+(int)(alfa*qGreen(gl)+(1-alfa)*qGreen(g));
                  data_iter++;

                  //niebieski
                  gh=abs(qBlue(gl)-qBlue(g));
                  gv=abs(qBlue(gl)-qBlue(w));
                  if(gh+gv!=0)
                  {
                    alfa=gv/(gh+gv);
                  }
                  else
                  {
                      alfa=0.5;
                  }
                  Bx=dedata[data_iter]+(int)(alfa*qBlue(gl)+(1-alfa)*qBlue(g));
                  data_iter++;
                  debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));
              }
              else if(hi!=0)  //krawedź gorna
              {
                  Rx=dedata[data_iter]+qRed(debitmap->pixel(hi-1,vi));
                  data_iter++;
                  Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi-1,vi));
                  data_iter++;
                  Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi-1,vi));
                  data_iter++;
                  debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));



              }
              else if(vi!=0)
              {
                  Rx=dedata[data_iter]+qRed(debitmap->pixel(hi,vi-1));
                  data_iter++;
                  Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi,vi-1));
                  data_iter++;
                  Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi,vi-1));
                  data_iter++;
                  debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));

              }
              else   //gorny lewy piksel
              {
                  Rx=dedata[data_iter];
                  data_iter++;
                  Gx=dedata[data_iter];
                  data_iter++;
                  Bx=dedata[data_iter];
                  data_iter++;
                  debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));
              }
              /*Tutaj otrzymujesz już zakodowane dane po wyjsciu z predyktora. Teraz są to shorty:
                Rx-zakodowane z kanału Red
                Gx-zakodowane z kanału Green
                Bx-zakodowanie z kanału Blue
              */
             //qDebug()<<"x="<<hi<<" y= "<<vi<<Rx<<" "<<Gx<<" "<<Bx<<"oryginalny pixel R="<<qRed(debitmap->pixel(hi,vi))<<" G="<<qGreen(debitmap->pixel(hi,vi))<<" B= "<<qBlue(debitmap->pixel(hi,vi));
        }
    }
}

void compresor::de_ALCM(predictor_header *ph)
{
    int v=0;  //iterator pionowy blokow
    int h=0;  //iterator poziomy blokow
    int vi;   //iterator pionowy wewnatrz bloku
    int hi;   //iterator poziomy wewnatrz bloku
    int data_iter=0;
    unsigned short size=ph->segment_size;
    debitmap = new QImage(ph->bitmap.width,ph->bitmap.height, QImage::Format_RGB32);
    int a;
    short Rx,Gx,Bx;
    short Rtemp,Gtemp, Btemp;
    float Wgl[3],Wg[3],Wgp[3],Ww[3],Www[3];

    for(v=0; v < debitmap->height(); v+=size)
    {
        for(h=0; h<debitmap->width() ; h+=size)
        {

              for(int i=0; i<3; i++)
              {
                 Wgl[i]=Wg[i]=Wgp[i]=Ww[i]=Www[i]=1.0/5.0;  //zerowanie wspolczynnikow

              }
              for(vi=v ; (vi<v+size) && (vi < debitmap->height()); vi++)
              {
                  for(hi=h; (hi<h+size) && (hi < debitmap->width()); hi++)
                  {

                        if((hi!=0) && (hi!=1) && (hi!=debitmap->width()-1) && (vi!= 0))   //blok nie na krawedzi
                        {
                            if(hi!=(h+size-1))
                            {
                            Rtemp=dedata[data_iter];
                            Rx=Rtemp+(int)(Wgl[0]*qRed(debitmap->pixel(hi-1,vi-1))+
                                                                 Wg[0]*qRed(debitmap->pixel(hi,vi-1))+
                                                                 Wgp[0]*qRed(debitmap->pixel(hi+1,vi-1))+
                                                                 Ww[0]*qRed(debitmap->pixel(hi-1,vi))+
                                                                 Www[0]*qRed(debitmap->pixel(hi-2,vi)));
                            data_iter++;
                            Gtemp=dedata[data_iter];
                            Gx=Gtemp+(int)(Wgl[0]*qGreen(debitmap->pixel(hi-1,vi-1))+
                                                                 Wg[0]*qGreen(debitmap->pixel(hi,vi-1))+
                                                                 Wgp[0]*qGreen(debitmap->pixel(hi+1,vi-1))+
                                                                 Ww[0]*qGreen(debitmap->pixel(hi-1,vi))+
                                                                 Www[0]*qGreen(debitmap->pixel(hi-2,vi)));
                            data_iter++;
                            Btemp=dedata[data_iter];
                            Bx=Btemp+(int)(Wgl[0]*qBlue(debitmap->pixel(hi-1,vi-1))+
                                                                 Wg[0]*qBlue(debitmap->pixel(hi,vi-1))+
                                                                 Wgp[0]*qBlue(debitmap->pixel(hi+1,vi-1))+
                                                                 Ww[0]*qBlue(debitmap->pixel(hi-1,vi))+
                                                                 Www[0]*qBlue(debitmap->pixel(hi-2,vi)));
                            data_iter++;
                            debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));
                            }
                            else
                            {
                                Rtemp=dedata[data_iter];
                                Rx=Rtemp+(int)(Wgl[0]*qRed(debitmap->pixel(hi-1,vi-1))+
                                                                     Wg[0]*qRed(debitmap->pixel(hi,vi-1))+
                                                                     Ww[0]*qRed(debitmap->pixel(hi-1,vi))+
                                                                     Www[0]*qRed(debitmap->pixel(hi-2,vi)));
                                data_iter++;
                                Gtemp=dedata[data_iter];
                                Gx=Gtemp+(int)(Wgl[0]*qGreen(debitmap->pixel(hi-1,vi-1))+
                                                                     Wg[0]*qGreen(debitmap->pixel(hi,vi-1))+
                                                                     Ww[0]*qGreen(debitmap->pixel(hi-1,vi))+
                                                                     Www[0]*qGreen(debitmap->pixel(hi-2,vi)));
                                data_iter++;
                                Btemp=dedata[data_iter];
                                Bx=Btemp+(int)(Wgl[0]*qBlue(debitmap->pixel(hi-1,vi-1))+
                                                                     Wg[0]*qBlue(debitmap->pixel(hi,vi-1))+
                                                                     Ww[0]*qBlue(debitmap->pixel(hi-1,vi))+
                                                                     Www[0]*qBlue(debitmap->pixel(hi-2,vi)));
                                data_iter++;
                                debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));
                            }
                            //modyfikacja wspolczynnikow

                            for(int i=0; i<3; i++)
                            {
                                a=1;
                                if((Rtemp<0) && (i==0))
                                {
                                    a=-1;
                                }
                                else if((Gtemp<0)&&(i==1))
                                {
                                    a=-1;
                                }
                                else if((Btemp<0)&&(i==2))
                                {
                                    a=-1;
                                }

                                //ustalanie najwiekszego zgodnie z priorytetami
                                if((Wgl[i]>Wg[i])&&(Wgl[i]>Wgp[i])&&(Wgl[i]>Ww[i])&&(Wgl[i]>Www[i]))//Wgl najwiekszy
                                {
                                    Wgl[i]+=a*1.0/256.0;
                                }
                                else if((Wg[i]>Wgl[i])&&(Wg[i]>Wgp[i])&&(Wg[i]>Ww[i])&&(Wg[i]>Www[i]))
                                {
                                    Wg[i]+=a*1.0/256.0;
                                }
                                else if((Wgp[i]>Wgl[i])&&(Wgp[i]>Wg[i])&&(Wgp[i]>Ww[i])&&(Wgp[i]>Www[i]))
                                {
                                    Wgp[i]+=a*1.0/256.0;
                                }
                                else if((Ww[i]>Wgl[i])&&(Ww[i]>Wg[i])&&(Ww[i]>Wgp[i])&&(Ww[i]>Www[i]))
                                {
                                    Ww[i]+=a*1.0/256.0;
                                }
                                else if((Www[i]>Wgl[i])&&(Www[i]>Wg[i])&&(Www[i]>Wgp[i])&&(Www[i]>Ww[i]))
                                {
                                    Www[i]+=a*1.0/256.0;
                                }
                                //ustalanie najmniejszego zgodnie z priorytetami

                                if((Wgl[i]<Wg[i])&&(Wgl[i]<Wgp[i])&&(Wgl[i]<Ww[i])&&(Wgl[i]<Www[i]))//Wgl najmniejszy
                                {
                                    Wgl[i]-=a*1.0/256.0;
                                }
                                else if((Wg[i]<Wgl[i])&&(Wg[i]<Wgp[i])&&(Wg[i]<Ww[i])&&(Wg[i]<Www[i]))
                                {
                                    Wg[i]-=a*1.0/256.0;
                                }
                                else if((Wgp[i]<Wgl[i])&&(Wgp[i]<Wg[i])&&(Wgp[i]<Ww[i])&&(Wgp[i]<Www[i]))
                                {
                                    Wgp[i]-=a*1.0/256.0;
                                }
                                else if((Ww[i]<Wgl[i])&&(Ww[i]<Wg[i])&&(Ww[i]<Wgp[i])&&(Ww[i]<Www[i]))
                                {
                                    Ww[i]-=a*1.0/256.0;
                                }
                                else if((Www[i]<Wgl[i])&&(Www[i]<Wg[i])&&(Www[i]<Wgp[i])&&(Www[i]<Ww[i]))
                                {
                                    Www[i]-=a*1.0/256.0;
                                }

                            }

                        }
                        else if((hi==0) && (vi==0)) //gorny lewy przesyłany bezpośrednio
                        {
                            Rx=dedata[data_iter];
                            data_iter++;
                            Gx=dedata[data_iter];
                            data_iter++;
                            Bx=dedata[data_iter];
                            data_iter++;
                            debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));

                        }

                        else if(vi==0) //krawedz górna
                        {
                            Rx=dedata[data_iter]+qRed(debitmap->pixel(hi-1,vi));
                            data_iter++;
                            Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi-1,vi));
                            data_iter++;
                            Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi-1,vi));
                            data_iter++;
                            debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));

                            //qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi bocznej";
                        }
                        else if(hi==0)  //krawedz skrajnie lewa
                        {
                            Rx=dedata[data_iter]+qRed(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            Gx=dedata[data_iter]+qGreen(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            Bx=dedata[data_iter]+qBlue(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));
                            //qDebug()<<hi<<" "<<vi<<" "<<qRed(debitmap->pixel(hi,vi))<<" "<<qRed(bitmap->pixel(hi,vi));
                           // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi gornej";
                        }
                        else if(hi==1)  //krawedz druga od lewej
                        {
                            Rx=dedata[data_iter]+0.5*qRed(debitmap->pixel(hi-1,vi))+0.5*qRed(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            Gx=dedata[data_iter]+0.5*qGreen(debitmap->pixel(hi-1,vi))+0.5*qGreen(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            Bx=dedata[data_iter]+0.5*qBlue(debitmap->pixel(hi-1,vi))+0.5*qBlue(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));
                           // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi gornej";
                        }
                        else if(hi==bitmap->width()-1)  //skrajnie prawa
                        {
                            Rx=dedata[data_iter]+0.5*qRed(debitmap->pixel(hi-1,vi))+0.5*qRed(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            Gx=dedata[data_iter]+0.5*qGreen(debitmap->pixel(hi-1,vi))+0.5*qGreen(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            Bx=dedata[data_iter]+0.5*qBlue(debitmap->pixel(hi-1,vi))+0.5*qBlue(debitmap->pixel(hi,vi-1));
                            data_iter++;
                            debitmap->setPixel(hi,vi,(0xFF<<24)+((Rx&0xFF)<<16)+((Gx&0xFF)<<8)+(Bx&0xFF));

                           // qDebug()<<"piksel o wspolrzednych x="<<hi <<" y="<<vi<<" jest na krawedzi gornej";
                        }

                        /*Tutaj otrzymujesz już zakodowane dane po wyjsciu z predyktora. Teraz są to shorty:
                          Rx-zakodowane z kanału Red
                          Gx-zakodowane z kanału Green
                          Bx-zakodowanie z kanału Blue
                        */
                  }
              }
        }
    }
}

unsigned short compresor::float_to_short(float value)
{
    unsigned short new_value=0;
    if(value<0)
    {
        new_value=new_value|0x8000;  //ustawienie jedynki na pierwszym bicie
        value*=-1;
    }
    new_value += (static_cast<short>(value*(1<<12)))&0x7FFF;
    return new_value;
}

float compresor::short_to_float(unsigned short value)
{
    float new_value;
    new_value=value&0x7FFF;
    new_value=new_value/(1<<12);
    if((value&0x8000)!=0)
    {
        new_value*=-1;
    }
    return new_value;
}

void compresor::predictor_change(int new_predictor)
{
    predictor = new_predictor;
}

void compresor::run()
{
    while(1)
    {
        if (compression_start)
        {
            //tutaj funkcje które kompresują obraz
            switch (predictor)
            {
                case 0:
                    compresor_forward2(block_size);
                break;
                case 1:
                    compresor_forward3(block_size);
                break;
                case 2:
                    PNGm5();
                break;
                case 3:
                    MED_MAP();
                break;
                case 4:
                    Graham();
                break;
                case 5:
                    DARC();
                break;
                case 6:
                    ALCM(block_size);
                break;
            }
            //qDebug() << "huffman";
            huffman.compress(parameters, data, compression_path);
            emit bitmap_compressed();
            emit new_data(data);
                //compresor_forward2(block_size);
            compression_start = false;                //koniecznie na końcu
        }
        if (decompression_start)
        {
            //aaa
            //qDebug() << "decompresion start";
            dedata = huffman.decompress(parameters, decompression_path);
            switch (parameters->compression_type)
            {
                case 0:
                    de_compresor_forward2(parameters);
                break;
                case 1:
                    de_compresor_forward3(parameters);
                break;
                case 2:
                    de_PNGm5(parameters);
                break;
                case 3:
                    de_MED_MAP(parameters);
                break;
                case 4:
                    de_Graham(parameters);
                break;
                case 5:
                    de_DARC(parameters);
                break;
                case 6:
                    de_ALCM(parameters);
                break;
            }
            //
            emit bitmap_decompressed(debitmap);
            //delete data;
            decompression_start = false;              //koniecznie na końcu
        }
        sleep(1);
     }
}
